package com.yumingjaing.flow.jdk;

import java.util.concurrent.Flow;

public class MySubscriber implements Flow.Subscriber<Integer> {

    private Flow.Subscription subscription;

    @Override
    // 绑定好订阅关系后，就会触发这个方法
    public void onSubscribe(Flow.Subscription subscription) {
        this.subscription = subscription;
        this.subscription.request(10);
    }

    @Override
    public void onNext(Integer item) {
        System.out.println(Thread.currentThread().getName() + "：接收到数据流：" + item);
    }

    @Override
    public void onError(Throwable throwable) {
        System.out.println(Thread.currentThread().getName() + "：接收消息出现异常：" + throwable.getMessage());
    }

    @Override
    public void onComplete() {
        System.out.println(Thread.currentThread().getName() + "：当前订阅者要求接收的消息全部处理完毕。");
    }
}